home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / com / othernet / fidonet / checktic / checktic.c next >
C/C++ Source or Header  |  1994-10-04  |  7KB  |  187 lines

  1. /*******************************************************************************/
  2. /*                                                                             */
  3. /* CHECKTIC                                                                    */
  4. /*                                                                             */
  5. /* workaround for the buggy CRC-checksum-compare-function                      */
  6. /* in TICKer-ST v.1.04beta from Rayko Balun                                    */
  7. /*                                                                             */
  8. /* Author: Markus Fischer                                                      */
  9. /*                                                                             */
  10. /* started: 25.09.1994                                                         */
  11. /*                                                                             */
  12. /* changed: 1.10.1994: now all leading zeros in the crc-checksum will be       */
  13. /*                     removed (although such TIC-files haven't been seen here)*/
  14. /*                     translated comments from german to english              */
  15. /*            4.10.1994: bugfix: check for trailing backslash pointed to wrong   */
  16. /*                     position in the parameter-string                        */
  17. /*                                                                             */
  18. /* -> inbound_path: path to folder containing the TIC-files which should be    */
  19. /*                  searched for CRC-checksums with leading zero               */
  20. /*                                                                             */
  21. /* <- returnvalue: 0 - program termination without error, but:                 */
  22. /*                     - big TIC-files (> 64KB) have not been searched         */
  23. /*                     - no TIC-files have been found                          */
  24. /*                 1 - incorrect number of parameters or parameter too long    */
  25. /*                 2 - insufficient memory for internal buffer available       */
  26. /*                                                                             */
  27. /* Limitations: - parameter <inbound_path> will not be checked for validation  */
  28. /*                or existence                                                 */
  29. /*              - Program works with internal buffer of static size (64KB)     */
  30. /*                to examine TIC-files in memory. But TIC-files of that size   */                 Allerdings duerften so grosse TIC-Files eher selten     */
  31. /*                seem very rare to me.                                        */
  32. /*                                                                             */
  33. /*******************************************************************************/
  34.  
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <ext.h>
  38. #include <string.h>
  39.  
  40. #define    bufsize    64*1024L
  41.  
  42. struct    ffblk    fblock;
  43. int        lastentry;                /* result of search for TIX-file      */
  44. size_t    pathlength;                /* Pathlength                         */
  45. char    inboundpath[128];        /* Path from parameter, if needed with
  46.                                    additonal terminating backslash    */
  47. char    searchpattern[128];        /* Pathline with search-pattern added */
  48. char    filename[128];            /* complete path with filename        */
  49. char    *p;                        /* pointer at begin of the buffer     */
  50. char    *crcpos;                /* pointer at the begin of the search
  51.                                    pattern in the buffer              */
  52. size_t    counter;                /* number of chars read into buffer   */ 
  53.  
  54. FILE    *fp;
  55.  
  56. char    pattern[] = "\nCRC ";    /* search pattern for the CRC-line in 
  57.                                    TIC-file */
  58. size_t    offset;                 /* length of the search pattern       */
  59.  
  60. main (int argc, char *argv[])
  61. {
  62.     printf ("CheckTIC v.1.0.1 - workaround for TICKer-ST v. 1.04beta from Rayko Balun\n");
  63.     printf ("Copyright 1994 by Markus Fischer. All rights reserved.\n");
  64.     printf ("Compiled on %s at %s with PureC v.1.1\n\n",__DATE__,__TIME__);
  65.  
  66.     /* no parameter or more than one? */
  67.     
  68.     if (argc != 2)
  69.     {
  70.         printf ("ERROR: call program 'checktic.ttp <inbound_path>'\n");
  71.         return (1);
  72.     }
  73.  
  74.     /* path-parameter to TIC-files too long (128 chars minus 12 chars) */
  75.      
  76.     if ((pathlength = strlen (argv[1])) > 116)
  77.     {
  78.         printf ("ERROR: parameter <inbound_path> too long\n");
  79.         return (1);
  80.     }
  81.     
  82.     strcpy (inboundpath,argv[1]);
  83.     
  84.     if (inboundpath[pathlength-1] != '\\')
  85.     {
  86.         strcat (inboundpath, "\\");            /* append missing backslash */
  87.         pathlength+=2;
  88.         if (pathlength > 116)
  89.         {
  90.             printf ("ERROR: parameter <inbound_path> too long\n");
  91.             return (1);
  92.         }
  93.     }
  94.  
  95.     /* create search-pattern */
  96.     
  97.     strcpy (searchpattern,inboundpath);
  98.     strcat (searchpattern,"*.TIC");
  99.  
  100.     /* search TIC-files */
  101.     
  102.     if ((lastentry = findfirst (searchpattern,&fblock,0)) != 0)
  103.     {
  104.         printf ("No TIC-files found\n");
  105.         return (0);
  106.     }
  107.  
  108.     /* reserve static buffer for TIC-file */
  109.  
  110.     if ((p = (char *) malloc (sizeof (char)* bufsize)) == NULL)
  111.     {
  112.         printf ("ERROR: insufficient memory\n");
  113.         return (2);
  114.     }
  115.     
  116.     offset = strlen (pattern);    /* offset from start of search-
  117.                                    pattern to next char             */
  118.     
  119.     while (!lastentry)
  120.     {
  121.         /* is TIC-file bigger than reserved buffer? */
  122.  
  123.         if (fblock.ff_fsize > bufsize)
  124.         {
  125.             printf ("%s too large for buffer - CRC-check skipped\n",fblock.ff_name);
  126.         }
  127.         else
  128.         {
  129.             /* create complete pathline */
  130.  
  131.             strcpy (filename,inboundpath);
  132.             strcat (filename,fblock.ff_name);
  133.  
  134.             /* read file into buffer */
  135.  
  136.             if ((fp = fopen (filename,"r")) != NULL)
  137.             {
  138.                 counter = fread (p, sizeof (char), fblock.ff_fsize, fp);
  139.                 fclose (fp);
  140.                 p[counter+1] = '\0';            /* restrict buffer at the end */
  141.  
  142.                 /* look for line with CRC-checksum in buffer */
  143.  
  144.                 if ((crcpos = strstr (p, pattern)) != NULL)
  145.                 {
  146.                     /* If the checksum starts with leading zero,
  147.                        TICKer-ST reports a checksum-error
  148.                        -> "workaround": remove the zero from the TIC-file           */
  149.                     
  150.                     /* examine the char following the search-pattern being the "offending" zero */
  151.                     if (crcpos[offset] == '0')    
  152.                     {
  153.                         /* move all chars behind the leading zero one 
  154.                            one char forward until no leading zeros are found
  155.                            and rewrite the file afterwards                          */
  156.                         while (crcpos[offset] == '0')
  157.                         {
  158.                             strcpy (crcpos+offset*sizeof(char),crcpos+(offset+1)*sizeof(char));
  159.                             counter--;
  160.                         }
  161.  
  162.                         if ((fp = fopen (filename,"w")) != NULL)
  163.                         {
  164.                             fwrite (p, sizeof (char), counter * sizeof (char), fp);
  165.                             fclose (fp);
  166.                             printf ("%s: CRC-checksum adjusted\n",fblock.ff_name);
  167.                         }
  168.                         else
  169.                             printf ("Couldn't rewrite %s - will lead to CRC-error when running TICKer-ST\n",fblock.ff_name);
  170.                     }
  171.                 }
  172.                 else
  173.                 {
  174.                     printf ("%s contains no CRC-line!\n",fblock.ff_name);
  175.                 }
  176.                 
  177.             }
  178.             else
  179.             {
  180.                 printf ("Couldn't open %s - CRC-check skipped\n",fblock.ff_name);
  181.             }
  182.         }
  183.         lastentry = findnext (&fblock);        /* is there another TIC-file? */
  184.     }
  185.     free (p);                                /* release buffer */
  186.     return (0);
  187. }